package com.company.project.web;
import com.company.project.core.LayUiTableJson;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.SysUser;
import com.company.project.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
* Created by CodeGenerator on 2020/04/18.
*/
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    private final static String DEFAULT_PWD = "123456";

    @GetMapping("/page/manage")
    public ModelAndView manage(){
        ModelAndView modelAndView = new ModelAndView("/workspace/user/userList.html");
        return modelAndView;
    }

    @GetMapping("/page/detail")
    public ModelAndView detail(){
        ModelAndView modelAndView = new ModelAndView("/workspace/user/userAdd.html");
        return modelAndView;
    }

    @PostMapping("/add")
    public Result add(SysUser sysUser) {
        SysUser userinfo = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysUser.setCreator(userinfo.getId());
        sysUser.setUpdateTime(new Date());
        sysUser.setUpdator(userinfo.getId());
        sysUser.setStatus((short)1);
        sysUser.setPwd(DEFAULT_PWD);
        sysUser.setSalt(UUID.randomUUID().toString());
        sysUserService.save(sysUser);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        sysUserService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(SysUser sysUser) {
        SysUser userinfo = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysUser.setUpdateTime(new Date());
        sysUser.setUpdator(userinfo.getId());
        sysUserService.update(sysUser);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        SysUser sysUser = sysUserService.findById(id);
        return ResultGenerator.genSuccessResult(sysUser);
    }

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SysUser> list = sysUserService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @RequestMapping("/list/condition")
    public LayUiTableJson getConditionList(String userName, String keyword,
                                         @RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer size){
        Result responseResult = sysUserService.getConditionList(userName,keyword, currentPage, size);
        if (ResultGenerator.isSuccess(responseResult)){
            return new LayUiTableJson(0,null,responseResult.getPage().getAllRow(),(List<SysUser>)responseResult.getData());
        }else{
            return new LayUiTableJson(1,null,0,null);
        }
    }

    /**
     * 更改用户状态
     * @param userId 用户Id
     * @param status 用户状态
     * @return
     */
    @PostMapping("/changeStatus")
    public Result changeStatus(@RequestParam Integer userId, @RequestParam Integer status){
        return sysUserService.changeStatus(userId,status);
    }

    /**
     * 用户修改密码
     * @param
     * @return
     */
    @PostMapping("/changePassword")
    public Result changePassword(@RequestParam String oldPwd, @RequestParam String newPwd){
        return sysUserService.changePassword(oldPwd,newPwd);
    }
}
